"use strict";
cc._RF.push(module, 'f11acgVgU1AQqD2zO7INjrB', 'data_control');
// scripts/engines/libs/data_control.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AStarPot = void 0;
/**
 * class 数据控制集
 * */
var NUMBER_FORMATS = [
    { numValue: 1e3, text: 'K' },
    { numValue: 1e6, text: 'M' },
    { numValue: 1e9, text: 'G' },
    { numValue: 1e12, text: 'T' },
    { numValue: 1e15, text: 'P' },
    { numValue: 1e18, text: 'E' },
    { numValue: 1e21, text: 'Z' },
    { numValue: 1e24, text: 'Y' },
    { numValue: 1e27, text: 'B' },
    { numValue: 1e30, text: 'N' },
    { numValue: 1e33, text: 'D' },
    { numValue: 1e36, text: 'aa' },
    { numValue: 1e39, text: 'bb' },
    { numValue: 1e42, text: 'cc' },
    { numValue: 1e45, text: 'dd' },
    { numValue: 1e48, text: 'ee' },
    { numValue: 1e51, text: 'ff' },
    { numValue: 1e54, text: 'gg' },
    { numValue: 1e57, text: 'hh' },
    { numValue: 1e60, text: 'ii' },
    { numValue: 1e63, text: 'jj' },
    { numValue: 1e66, text: 'kk' },
    { numValue: 1e69, text: 'll' },
    { numValue: 1e72, text: 'mm' },
    { numValue: 1e75, text: 'nn' },
    { numValue: 1e78, text: 'oo' },
    { numValue: 1e81, text: 'pp' },
    { numValue: 1e84, text: 'qq' },
    { numValue: 1e87, text: 'rr' },
    { numValue: 1e90, text: 'ss' },
    { numValue: 1e93, text: 'tt' },
    { numValue: 1e96, text: 'uu' },
    { numValue: 1e99, text: 'vv' }
];
/*
* 载、十载、百载、千载
极、十极、百极、千极
恒河沙、十恒河沙、百恒河沙、千恒河沙
阿僧祗、十阿僧祗、百阿僧祗、千阿僧祗
那由他、十那由他、百那由他、千那由他
不可思议、十不可思议、百不可思议、千不可思议
无量、十无量、百无量、千无量
大数、十大数、百大数、千大数*/
var data_control = /** @class */ (function () {
    function data_control() {
    }
    /**
     * @param Len 长度
     * @return ID
     */
    data_control.getRandId = function (Len) {
        if (Len === void 0) { Len = 16; }
        var id = '';
        for (var i = 0; i < Len; i++) {
            var ip = Math.floor(Math.random() * this.IDs.length);
            id += this.IDs[ip];
        }
        return id;
    };
    /**
     * 使值取区间内
     * @param value 源值
     * @param minValue 最小值
     * @param maxValue 最大值
     * */
    data_control.getValueOfAreaMinMax = function (value, minValue, maxValue) {
        if (value < minValue)
            return minValue;
        if (value > maxValue)
            return maxValue;
        return value;
    };
    /**
     * 使值循环于区间内
     * @param value 源值
     * @param minValue 最小值
     * @param maxValue 最大值
     * */
    data_control.getNumLoopOfAreaMinMax = function (value, minValue, maxValue) {
        if (value < minValue)
            return maxValue;
        if (value > maxValue)
            return minValue;
        return value;
    };
    /**
     * 求和
     * */
    data_control.sum = function (array) {
        var sumValue = 0;
        for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
            var num = array_1[_i];
            sumValue += num;
        }
        return sumValue;
    };
    /**
     * 从数组里随机取一个成员返回
     * @param ary 源数组
     * */
    data_control.getRandItemOfArray = function (ary) {
        var i = Math.floor(Math.random() * ary.length);
        return ary[i];
    };
    /**
     * 从区间里取值 [最小值,最大值]
     * */
    data_control.getRandNumberOfArrayTwo = function (array) {
        if (array.length != 2 || array[0] > array[1]) {
            console.error('#40 data_control 错误的区间', array);
            return NaN;
        }
        return array[0] + Math.random() * (array[1] - array[0]);
    };
    /**
     * 通过一组权重获取数组获取随机下标
     * @param weights 权重数组
     */
    data_control.getRandIPosByWeights = function (weights) {
        //
        var weightSum = this.sum(weights);
        var randSum = Math.random() * weightSum;
        //
        for (var i = 0; i < weights.length; i++) {
            randSum -= weights[i];
            if (randSum <= 0) {
                return i;
            }
        }
        console.error('#114 data_control 不可能的错误', weights);
        return NaN;
    };
    /**
     * 对数组进行乱序操作
     * @param array 源数组
     */
    data_control.randomSortArray = function (array) {
        array.sort(function (a, b) {
            return Math.random() - 0.5;
        });
        return array;
    };
    /**
     * @param {Number} num
     * @return {String} 单位格式
     * */
    data_control.getNumberFormat = function (num, fixedLen) {
        if (fixedLen === void 0) { fixedLen = 2; }
        //常量里是从小到大的，所以要反序
        var ary = NUMBER_FORMATS;
        for (var i = ary.length - 1; i >= 0; i--) {
            var _a = ary[i], numValue = _a.numValue, text = _a.text;
            if (num >= numValue) {
                return "" + Number((num / numValue).toFixed(fixedLen)) + text;
            }
        }
        return '' + Number(Number(num).toFixed(fixedLen));
    };
    data_control.getNumberSplitCharString = function (num, char) {
        if (char === void 0) { char = ','; }
        var n = String(Math.floor(num));
        n = n.replace(/(?=(?!\b)(?:\d{3})+(?!\d))/g, char);
        return n;
    };
    /**
     * 返回一个保留指定位数小数的字符串
     * @param srcNumber 源数值
     * @param tail 保留的位数，默认两位
     * @param isClearLastZero 是否去除小数点后末尾多余的零 默认否
     * @returns 最终数值字符表现
     */
    data_control.keepDecimals = function (srcNumber, tail, isClearLastZero) {
        if (tail === void 0) { tail = 2; }
        if (isClearLastZero === void 0) { isClearLastZero = false; }
        //
        var srcString = String(srcNumber);
        var strArray = srcString.split('.');
        //
        var left = strArray[0];
        var right = strArray[1] || '';
        //先去保留
        if (right.length > tail) {
            right = right.substring(0, tail);
        }
        //小数点后面末尾的0去除
        if (isClearLastZero) {
            var rightNumber = Number(right);
            //小数点后面没值
            if (rightNumber == 0) {
                return left;
            }
        }
        else { //不去除则补0
            right = right.padEnd(tail, '0');
        }
        return left + "." + right;
    };
    /**
     * 将数字转大写
     * */
    data_control.getChineseNum = function (str) {
        str = String(str);
        if (!/^([1-9]\d*)/.test(str)) {
            return '非法数据';
        }
        var unit = '千百十亿千百十万千百十个';
        if (str.length > unit.length) {
            return '数据过长';
        }
        var newStr = '';
        var nLength = str.length;
        unit = unit.substr(unit.length - nLength);
        for (var i = 0; i < nLength; i++) {
            newStr += '零一二三四五六七八九'.charAt(Number(str[i])) + unit.charAt(i);
        }
        console.log(newStr);
        newStr = newStr.substr(0, newStr.length - 1);
        console.log(newStr);
        newStr = newStr.replace(/零(千|百|十)/g, '零').replace(/(零)+/g, '零').replace(/零(亿|万)/g, '$1');
        return newStr;
    };
    /**
     * @param timeNum 秒
     * return 返回 hh:mm:ss 格式 如 3600秒返回 01:00:00
     * */
    data_control.getTimeFormatClock = function (timeNum) {
        var m = 60 * 1;
        var t = 60 * m;
        var str = '';
        //时
        var strT = Math.floor(timeNum / t);
        if (strT > 0) {
            str += String(strT).padStart(2, '0') + ":";
        }
        //分
        var strM = Math.floor(timeNum % t / m);
        str += String(strM).padStart(2, '0') + ":";
        //秒
        str += String(timeNum % m).padStart(2, '0');
        return str;
    };
    /**
     * 获取当天的日期格式
     * */
    data_control.getCurrDateDayString = function () {
        var d = new Date();
        var str = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate();
        return str;
    };
    data_control.MinusToHours = function (minus) {
        var hour = Math.floor(minus / 60) ? Math.floor(minus / 60) + "\u5C0F\u65F6" : '';
        var minu = (minus % 60) ? (minus % 60) + "\u5206\u949F" : '';
        return "" + hour + minu;
    };
    /**
     * 输入两个向量，计算第一个相对于第二个的夹角∠
     * @param targetVec 目标向量
     * @param originVec 基准向量,默认为（0，1）
     */
    data_control.GetAngle = function (targetVec, originVec) {
        if (originVec === void 0) { originVec = cc.v2(0, 1); }
        var nor = cc.v2(targetVec.normalize());
        var nor2 = cc.v2(originVec.normalize());
        var acos = nor.dot(nor2);
        var cross = nor.cross(nor2); //this.PointCross(nor,nor2) ;//
        var angle = Math.acos(acos);
        if (cross == 0) {
            cross = acos < 0 ? 1 : 0;
        }
        angle *= Math.sign(cross);
        angle *= 180 / Math.PI;
        // console.log(`nor:${nor.x},${nor.y}\nnor2:${nor2.x},${nor2.y}\nacos:${acos}\ncross:${cross}\nangle:${angle}`);
        // angle = Math.round(angle);
        if (isNaN(angle)) {
            angle = acos < 0 ? 180 : 0;
        }
        return angle;
    };
    /**计算两个点的角度 */
    data_control.getV2Angle = function (pos1, pos2) {
        var theangle = Math.atan2(pos1.y - pos2.y, pos1.x - pos2.x); //弧度  
        var theta = theangle * 180 / Math.PI; //角度  
        // console.info("theta",theta)
        return theta;
    };
    data_control.GetWorldPos = function (localPos) {
        var res = localPos.parent.convertToWorldSpaceAR(localPos.getPosition());
        res.addSelf(cc.v2(cc.winSize.width * -0.5, cc.winSize.height * -0.5));
        return res;
    };
    data_control.AllArraySum = function (arr) {
        var res = cc.v2();
        for (var i = 0; i < arr.length; i++) {
            res.x += arr[i].x;
            res.y += arr[i].y;
        }
        return res;
    };
    data_control.GetLineCrossLine = function (x1, y1, x2, y2, x3, y3, x4, y4) {
        if (!(Math.min(x1, x2) <= Math.max(x3, x4) && Math.min(y3, y4) <= Math.max(y1, y2) && Math.min(x3, x4) <= Math.max(x1, x2) && Math.min(y1, y2) <= Math.max(y3, y4))) {
            return false;
        }
        var u, v, w, z;
        u = (x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1);
        v = (x4 - x1) * (y2 - y1) - (x2 - x1) * (y4 - y1);
        w = (x1 - x3) * (y4 - y3) - (x4 - x3) * (y1 - y3);
        z = (x2 - x3) * (y4 - y3) - (x4 - x3) * (y2 - y3);
        return (u * v <= 0.00000001 && w * z <= 0.00000001);
    };
    data_control.numberInTheArea = function (rootNum, min, max) {
        return rootNum >= min && rootNum <= max;
    };
    data_control.GetDirEqual = function (dir1, dir2) {
        if (dir2 == null || dir1 == null) {
            return false;
        }
        return (dir1.x == dir2.x) && (dir2.y == dir1.y);
    };
    /**判断points1是否包含于points2*/
    data_control.checkHasBaoHan = function (points1, points2) {
        for (var _i = 0, points1_1 = points1; _i < points1_1.length; _i++) {
            var point = points1_1[_i];
            if (!cc.Intersection.pointInPolygon(point, points2))
                return false;
        }
        return true;
    };
    /**判断points1与points2相交 包含也算相交*/
    data_control.checkHasXJ = function (points1, points2) {
        return cc.Intersection.polygonPolygon(points1, points2);
    };
    /**
     * 获取贝塞尔曲线 插值点
     * @param posList 坐标列表：起码三个点
     * @param t
     */
    data_control.getBerziPos = function (posList, t) {
        var xList = [];
        var yList = [];
        for (var _i = 0, posList_1 = posList; _i < posList_1.length; _i++) {
            var pos = posList_1[_i];
            xList.push(pos.x);
            yList.push(pos.y);
        }
        return cc.v2(this.getBerziNum(xList, t), this.getBerziNum(yList, t));
    };
    data_control.getBerziNum = function (numList, t) {
        if (numList.length == 1) {
            return numList[0];
        }
        var chazhi = [];
        for (var i = 0, len = numList.length; i < len; i++) {
            var p0 = numList[i];
            var p1 = numList[i + 1];
            if (p0 != null && p1 != null) {
                chazhi.push((1 - t) * p0 + t * p1);
            }
            else {
                return this.getBerziNum(chazhi, t);
            }
        }
    };
    /**
     * A*寻路
     * @param map 地图二维数组
     * @param startPot 寻路起始点
     * @param endPot 寻路终点
     * @param openList 开合队列 默认传startPot
     * @param closeList 闭合队列
     * @param findNextPos 下个节点可走路径迭代数组
     * @param trafficConditions 可行走规则
     * @param outPathPots  out参数 最终路径
     * @return 最终路径
     */
    data_control.aStarPathFind = function (map, startPot, endPot, openList, closeList, findNextPos, trafficConditions, outPathPots) {
        /*
         *   调用示例
            const list: AStarPot[] = [];
            const openList = [new AStarPot(0, 0, null)];
            const startPot = new AStarPot(0, 0, null);
            const endPot = new AStarPot(this.cellSize - 1, this.cellSize - 1, null);
            const findPots = [
                new AStarPot(0, 1, null),
                new AStarPot(0, -1, null),
                new AStarPot(1, 0, null),
                new AStarPot(-1, 0, null),
            ];
            data_control.aStarPathFind(
                this.m_desk,
                startPot,
                endPot,
                openList,
                {},
                findPots,
                currFind => {
                    return currFind.type != PotType.STOP;
                },
                list
            );
        */
        //找到open队列里 gh距离最近的点
        if (openList.length > 0) {
            var fi = 0;
            var minDistance = null;
            for (var i = 0; i < openList.length; i++) {
                var currI = openList[i];
                // let distanceI = Math.sqrt(
                //     //Math.abs(currI.i - startPot.i)
                //     //Math.abs(currI.parentCount)
                //     Math.pow(Math.abs(currI.parentCount), 2) + Math.pow(Math.abs(currI.j - startPot.j), 2)
                // );
                var distanceS = Math.sqrt(Math.pow(Math.abs(currI.i - startPot.i), 2) + Math.pow(Math.abs(currI.j - startPot.j), 2));
                var distanceE = Math.sqrt(Math.pow(Math.abs(currI.i - endPot.i), 2) + Math.pow(Math.abs(currI.j - endPot.j), 2));
                var distanceI = distanceS + distanceE;
                if (minDistance == null || distanceI < minDistance) {
                    fi = i;
                    minDistance = distanceI;
                }
            }
            var fPotUnit = openList[fi];
            // O1HD.emit('drawOne', fPotUnit, cc.Color.GRAY);
            //到达目标点
            if (fPotUnit.i == endPot.i && fPotUnit.j == endPot.j) {
                //
                outPathPots.length = 0;
                var p = fPotUnit;
                //反向找出路径
                while (p) {
                    outPathPots.push(p);
                    p = p.parent;
                }
                //倒序即为路径
                outPathPots.reverse();
            }
            else {
                closeList[fPotUnit.i + "-" + fPotUnit.j] = true;
                // closeList.push(fPotUnit);
                openList.splice(fi, 1);
                //方向
                for (var _i = 0, findNextPos_1 = findNextPos; _i < findNextPos_1.length; _i++) {
                    var dir = findNextPos_1[_i];
                    var cuDir = new AStarPot(fPotUnit.i + dir.i, fPotUnit.j + dir.j, fPotUnit);
                    //边界外 不加
                    if (cuDir.i >= map.length || cuDir.i < 0 || cuDir.j >= map[0].length || cuDir.j < 0)
                        continue;
                    //不满足条件格子 不加
                    if (!trafficConditions(map[fPotUnit.i][fPotUnit.j], map[cuDir.i][cuDir.j]))
                        continue;
                    //已经在关闭队列 不加
                    if (closeList[cuDir.i + "-" + cuDir.j])
                        continue;
                    //不在打开队列 加入
                    if (!cuDir.inList(openList)) {
                        openList.push(cuDir);
                    }
                }
                //打开队列有点
                if (openList.length > 0) {
                    this.aStarPathFind(map, startPot, endPot, openList, closeList, findNextPos, trafficConditions, outPathPots);
                }
            }
        }
        else {
            console.log('没有到达目标终点');
            // outPathPots.length = 0;
        }
        return outPathPots;
    };
    /**
     * 获得随机数（包含小数部分）
     * @param minNum
     * @param maxNum
     * @returns
     */
    data_control.randomNum = function (minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return Math.random() * minNum + 1;
            case 2:
                return Math.random() * (maxNum - minNum) + minNum;
            default:
                return 0;
        }
    };
    /**
     * 获得随机数，只取整数部分
     * @param minNum
     * @param maxNum
     * @returns
     */
    data_control.randomNumInt = function (minNum, maxNum) {
        var num = this.randomNum(minNum, maxNum);
        return Math.round(num);
    };
    /**
     * 随机获得多个随机数
     * @param minNum
     * @param maxNum
     * @param count
     * @returns
     */
    data_control.randomIntNum = function (minNum, maxNum, count) {
        var space = maxNum - minNum + 1;
        if (count > space) {
            count = space;
        }
        var retNum = [];
        while (count > 0) {
            var num = this.randomNumInt(minNum, maxNum);
            if (retNum.indexOf(num) == -1) {
                retNum.push(num);
                count--;
            }
        }
        return retNum;
    };
    data_control.IDs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    return data_control;
}());
exports.default = data_control;
var AStarPot = /** @class */ (function () {
    function AStarPot(i, j, parent) {
        this.i = i;
        this.j = j;
        this.parent = parent;
        this.parentCount = 0;
        if (parent) {
            this.parentCount = parent.parentCount + 1;
        }
    }
    AStarPot.prototype.equals = function (other) {
        return other.i == this.i && other.j == this.j;
    };
    AStarPot.prototype.inList = function (list) {
        for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
            var p = list_1[_i];
            if (this.equals(p)) {
                return true;
            }
        }
        return false;
    };
    return AStarPot;
}());
exports.AStarPot = AStarPot;

cc._RF.pop();